/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Laurent WOUTERS
 */
public class LexicalContext {
    private Map<String, xowl.lang.actions.CodeVariable> codeVariables;
    private Map<String, xowl.lang.actions.QueryVariable> queryVariables;
    
    public Collection<xowl.lang.actions.CodeVariable> getCodeVariables() { return codeVariables.values(); }
    public Collection<xowl.lang.actions.QueryVariable> getQueryVariables() { return queryVariables.values(); }

    public LexicalContext() {
        codeVariables = new HashMap<String, xowl.lang.actions.CodeVariable>();
        queryVariables = new HashMap<String, xowl.lang.actions.QueryVariable>();
    }
    public LexicalContext(LexicalContext parent) {
        codeVariables = new HashMap<String, xowl.lang.actions.CodeVariable>(parent.codeVariables);
        queryVariables = new HashMap<String, xowl.lang.actions.QueryVariable>(parent.queryVariables);
    }

    public void put(xowl.lang.actions.CodeVariable var) {
        codeVariables.put(var.getName(), var);
    }

    public xowl.lang.actions.CodeVariable resolveCode(String name) {
        if (name.equals("this"))
            return xowl.lang.actions.CodeVariable.get_This();
        else if (name.equals("null"))
            return xowl.lang.actions.CodeVariable.get_Null();
        if (codeVariables.containsKey(name))
            return codeVariables.get(name);
        xowl.lang.actions.CodeVariable var = new xowl.lang.actions.CodeVariable();
        var.setName(name);
        codeVariables.put(name, var);
        return var;
    }
    public xowl.lang.actions.QueryVariable resolveQuery(String name) {
        if (queryVariables.containsKey(name))
            return queryVariables.get(name);
        xowl.lang.actions.QueryVariable var = new xowl.lang.actions.QueryVariable();
        var.setName(name);
        queryVariables.put(name, var);
        return var;
    }
}
